package com.xyy.saas.payment.util.mq;

import com.xyy.saas.payment.adpater.pingxx.domain.RefundDO;
import com.xyy.saas.payment.adpater.pingxx.domain.WithdrawalDO;
import com.xyy.saas.payment.callback.core.dto.CallbackMsg;
import com.xyy.saas.payment.callback.core.handler.OrderRefundHandler;
import com.xyy.saas.payment.callback.core.handler.WithdrawalHandler;
import com.xyy.saas.payment.common.RocketMqGroup;
import com.xyy.saas.payment.common.RocketMqTopic;
import com.xyy.saas.payment.dao.mapper.WithdrawOrderMapper;
import com.xyy.saas.payment.dao.model.WithdrawOrder;
import com.xyy.saas.payment.util.DateUtil;
import com.xyy.saas.payment.util.JSONUtils;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.common.UtilAll;
import org.apache.rocketmq.common.consumer.ConsumeFromWhere;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.ConsumeMode;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.apache.rocketmq.spring.core.RocketMQPushConsumerLifecycleListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 提现 - 兼容富民同步提现，为业务屏蔽富民同步异步提现的差异性
 */
@Service
@RocketMQMessageListener(topic = RocketMqTopic.TOPIC_SYNC_REFUND_CALLBACK, consumerGroup = RocketMqGroup.GROUP_SYNC_REFUND_CALLBACK, consumeMode = ConsumeMode.CONCURRENTLY)
public class SyncRefundConsumer implements RocketMQListener<MessageExt>, RocketMQPushConsumerLifecycleListener {
    private static final Logger LOGGER = LoggerFactory.getLogger(SyncRefundConsumer.class);

    @Resource
    private OrderRefundHandler orderRefundHandler;


    @Override
    public void onMessage(MessageExt message) {
        String body = new String(message.getBody());

        try {
            LOGGER.info("SyncRefundConsumer#onMessage, body={}", body);
            orderRefundHandler.callBackHandler(JSONUtils.parse2Object(body, RefundDO.class));
        }
        catch(Exception ex) {
            LOGGER.error(ex.getMessage() + ", body=" + body ,ex);
        }
    }

    @Override
    public void prepareStart(DefaultMQPushConsumer consumer) {
        consumer.setConsumeFromWhere(ConsumeFromWhere.CONSUME_FROM_FIRST_OFFSET);
        consumer.setConsumeTimestamp(UtilAll.timeMillisToHumanString3(System.currentTimeMillis()));
    }
}